{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Class Tournament"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import random as rand\n",
    "from tabulate import tabulate\n",
    "from Match import Match, name, print_history\n",
    "from Cooperate import Cooperate\n",
    "from Defect import Defect\n",
    "from Flipper import Flipper\n",
    "from TitForTat import TitForTat\n",
    "\n",
    "cooperate = Cooperate()\n",
    "defect = Defect()\n",
    "flipper = Flipper()\n",
    "titForTat = TitForTat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Import everyone's rules\n",
    "from AverageTitForTat import AverageTitForTat\n",
    "from CheckAndChoose import CheckAndChoose, np\n",
    "from dbTriCon import dbTriCon\n",
    "from fifty_good import fifty_is_good\n",
    "from Patrick import Patrick\n",
    "from scummyPrisoner import scummyPrisoner\n",
    "from Smart import random, Smart\n",
    "from suspiciousProbingTFT import random, SPTFT\n",
    "from TFTMemory import random, TFTMemory\n",
    "from TitForTwoTats import TitForTwoTats\n",
    "from Trustworthy import random, Trustworthy\n",
    "from ResponsiveTFTMod import ResponsiveTFTMod\n",
    "from suspiciousTitForTat import suspiciousTitForTat\n",
    "\n",
    "averageTitForTat = AverageTitForTat()\n",
    "checkAndChoose = CheckAndChoose()\n",
    "dBTriCon = dbTriCon()\n",
    "fifty_good = fifty_is_good()\n",
    "patrick = Patrick()\n",
    "scummy_Prisoner = scummyPrisoner()\n",
    "smart = Smart()\n",
    "susProbingTFT = SPTFT()\n",
    "tftMemory = TFTMemory()\n",
    "titForTwoTats = TitForTwoTats()\n",
    "trustworthy = Trustworthy()\n",
    "responsiveTFTMod = ResponsiveTFTMod()\n",
    "susTitForTat = suspiciousTitForTat()\n",
    "\n",
    "rule_list = [averageTitForTat, checkAndChoose, dBTriCon, fifty_good, patrick, scummy_Prisoner, smart, susProbingTFT, tftMemory, titForTwoTats, trustworthy, cooperate, defect, flipper, titForTat, responsiveTFTMod, susTitForTat]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class Tournament():\n",
    "\n",
    "    def __init__(self, rule_list, length, identical_match=False):\n",
    "        self.rule_list = rule_list\n",
    "        self.length = length\n",
    "        self.identical_match = identical_match\n",
    "        \n",
    "        self.match_scores = []\n",
    "        self.rule_scores = {}\n",
    "        self.win_diff = []\n",
    "        self.wins = []\n",
    "        \n",
    "        for rule in self.rule_list:\n",
    "            self.rule_scores[name(rule)] = []\n",
    "\n",
    "    def run(self):\n",
    "        for match in self.match_gen():\n",
    "            match.run()\n",
    "            score = match.score()\n",
    "#             self.rule_scores[name(match.rule0)][name(match.rule1)] = score[match.rule0.order]\n",
    "#             self.rule_scores[name(match.rule1)][name(match.rule0)] = score[match.rule1.order]\n",
    "#             self.match_scores[match.name] = score\n",
    "            self.match_scores.append((match.name, score))\n",
    "    \n",
    "            self.rule_scores[name(match.rule0)].append(score)\n",
    "            self.rule_scores[name(match.rule1)].append(score[::-1])\n",
    "\n",
    "    def match_gen(self):\n",
    "        ''' Generator function to return initialized matches from the list of rules'''\n",
    "        l = range(len(self.rule_list))\n",
    "        if self.identical_match:\n",
    "            n = 0\n",
    "        else:\n",
    "            n = 1\n",
    "        for i in l:\n",
    "            for j in l:\n",
    "                \n",
    "                if j < i+n:\n",
    "                    continue\n",
    "                else:\n",
    "                    new_match = Match(self.rule_list[i], self.rule_list[j], self.length)\n",
    "                    yield new_match\n",
    "                    \n",
    "    def all_scores(self):\n",
    "        \n",
    "        self.score_loss_wins()\n",
    "        #Who got the highest score\n",
    "        #Who got the lowest score\n",
    "        #Who got the most total points\n",
    "        #Who got the least total points\n",
    "        #Who got the most wins\n",
    "        #Who got the least wins\n",
    "        #Who lost by the least on average\n",
    "        pass\n",
    "        \n",
    "    \n",
    "    def sort_rule_score(self):\n",
    "        \n",
    "        [scores.sort(reverse=True) for key, scores in self.match_scores.items()]\n",
    "    \n",
    "    def score_loss_wins(self):\n",
    "        ''' Perform scorings based on number of wins/losses/draws. '''\n",
    "        for rule, scores in self.rule_scores.items():\n",
    "            new_scores = [(score[0]-score[1]) for score in scores]\n",
    "            self.win_diff.append((rule, new_scores))\n",
    "            \n",
    "            win = sum([1 if (score>0) else 0 for score in new_scores])\n",
    "            draw = sum([1 if (score==0) else 0 for score in new_scores])\n",
    "            loss = sum([1 if (score<0) else 0 for score in new_scores])\n",
    "            self.wins.append((rule, win, draw, loss))\n",
    "    \n",
    "    def score_points(self):\n",
    "        ''''''\n",
    "        self.match_scores\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "t = Tournament(rule_list, 100)\n",
    "t.run()\n",
    "t.all_scores()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Wins, Losses, and Draws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wins                               Draws                              Losses\n",
      "---------------------------------  ---------------------------------  ---------------------------------\n",
      "('Defect', 12, 4, 0)               ('suspiciousTitForTat', 5, 11, 0)  ('Cooperate', 0, 2, 14)\n",
      "('scummyPrisoner', 11, 4, 1)       ('TitForTat', 0, 8, 8)             ('TitForTwoTats', 0, 3, 13)\n",
      "('ResponsiveTFTMod', 10, 6, 0)     ('AverageTitForTat', 9, 7, 0)      ('Trustworthy', 5, 1, 10)\n",
      "('AverageTitForTat', 9, 7, 0)      ('dbTriCon', 3, 7, 6)              ('SPTFT', 6, 1, 9)\n",
      "('Patrick', 9, 3, 4)               ('ResponsiveTFTMod', 10, 6, 0)     ('fifty_is_good', 6, 1, 9)\n",
      "('CheckAndChoose', 8, 1, 7)        ('TFTMemory', 4, 5, 7)             ('Flipper', 5, 2, 9)\n",
      "('Smart', 8, 4, 4)                 ('Smart', 8, 4, 4)                 ('TitForTat', 0, 8, 8)\n",
      "('SPTFT', 6, 1, 9)                 ('scummyPrisoner', 11, 4, 1)       ('CheckAndChoose', 8, 1, 7)\n",
      "('fifty_is_good', 6, 1, 9)         ('Defect', 12, 4, 0)               ('TFTMemory', 4, 5, 7)\n",
      "('Trustworthy', 5, 1, 10)          ('TitForTwoTats', 0, 3, 13)        ('dbTriCon', 3, 7, 6)\n",
      "('suspiciousTitForTat', 5, 11, 0)  ('Patrick', 9, 3, 4)               ('Smart', 8, 4, 4)\n",
      "('Flipper', 5, 2, 9)               ('Cooperate', 0, 2, 14)            ('Patrick', 9, 3, 4)\n",
      "('TFTMemory', 4, 5, 7)             ('Flipper', 5, 2, 9)               ('scummyPrisoner', 11, 4, 1)\n",
      "('dbTriCon', 3, 7, 6)              ('CheckAndChoose', 8, 1, 7)        ('AverageTitForTat', 9, 7, 0)\n",
      "('Cooperate', 0, 2, 14)            ('Trustworthy', 5, 1, 10)          ('suspiciousTitForTat', 5, 11, 0)\n",
      "('TitForTat', 0, 8, 8)             ('SPTFT', 6, 1, 9)                 ('ResponsiveTFTMod', 10, 6, 0)\n",
      "('TitForTwoTats', 0, 3, 13)        ('fifty_is_good', 6, 1, 9)         ('Defect', 12, 4, 0)\n"
     ]
    }
   ],
   "source": [
    "s_w = sorted(t.wins, key=lambda x: x[1], reverse=True)\n",
    "s_d = sorted(t.wins, key=lambda x: x[2], reverse=True)\n",
    "s_l = sorted(t.wins, key=lambda x: x[3], reverse=True)\n",
    "\n",
    "headers = ['Wins', 'Draws', 'Losses']\n",
    "\n",
    "print(tabulate([[s_w[i], s_d[i], s_l[i]] for i in range(len(s_w))], headers=headers))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Raw Points"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Maximum Points in a Match"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Individual                           Collective\n",
      "-----------------------------------  ----------------------------------------------------\n",
      "('scummyPrisoner', [0, 500])         ('dbTriCon-Cooperate', [300, 300])\n",
      "('Defect', [0, 500])                 ('TitForTwoTats-Cooperate', [300, 300])\n",
      "('scummyPrisoner', [500, 0])         ('TitForTwoTats-TitForTat', [300, 300])\n",
      "('Defect', [0, 500])                 ('Cooperate-TitForTat', [300, 300])\n",
      "('fifty_is_good', [498, 3])          ('AverageTitForTat-TitForTwoTats', [302, 297])\n",
      "('Smart', [494, 9])                  ('AverageTitForTat-Cooperate', [302, 297])\n",
      "('CheckAndChoose', [490, 15])        ('TFTMemory-TitForTwoTats', [302, 297])\n",
      "('AverageTitForTat', [485, 10])      ('TFTMemory-Cooperate', [302, 297])\n",
      "('Smart', [17, 477])                 ('TitForTwoTats-suspiciousTitForTat', [297, 302])\n",
      "('AverageTitForTat', [471, 21])      ('Cooperate-suspiciousTitForTat', [297, 302])\n",
      "('Smart', [23, 468])                 ('dbTriCon-TitForTat', [299, 299])\n",
      "('CheckAndChoose', [454, 29])        ('SPTFT-Cooperate', [304, 294])\n",
      "('Defect', [20, 420])                ('dbTriCon-TitForTwoTats', [301, 296])\n",
      "('fifty_is_good', [418, 23])         ('dbTriCon-suspiciousTitForTat', [296, 301])\n",
      "('AverageTitForTat', [413, 33])      ('SPTFT-TitForTwoTats', [310, 285])\n",
      "('CheckAndChoose', [411, 36])        ('Patrick-TFTMemory', [328, 258])\n",
      "('scummyPrisoner', [406, 41])        ('CheckAndChoose-TitForTwoTats', [295, 290])\n",
      "('Flipper', [150, 400])              ('CheckAndChoose-TitForTat', [291, 291])\n",
      "('Flipper', [150, 400])              ('CheckAndChoose-suspiciousTitForTat', [288, 293])\n",
      "('ResponsiveTFTMod', [198, 368])     ('TitForTwoTats-Trustworthy', [240, 340])\n",
      "('ResponsiveTFTMod', [198, 368])     ('Trustworthy-Cooperate', [340, 240])\n",
      "('Patrick', [342, 237])              ('dbTriCon-Trustworthy', [242, 337])\n",
      "('Trustworthy', [240, 340])          ('Patrick-Cooperate', [342, 237])\n",
      "('Trustworthy', [340, 240])          ('TFTMemory-Trustworthy', [242, 337])\n",
      "('Trustworthy', [242, 337])          ('Patrick-TitForTwoTats', [336, 236])\n",
      "('Trustworthy', [242, 337])          ('TitForTwoTats-ResponsiveTFTMod', [198, 368])\n",
      "('Patrick', [336, 236])              ('Cooperate-ResponsiveTFTMod', [198, 368])\n",
      "('Smart', [336, 146])                ('CheckAndChoose-Patrick', [240, 325])\n",
      "('ResponsiveTFTMod', [166, 331])     ('Trustworthy-TitForTat', [283, 278])\n",
      "('Flipper', [180, 330])              ('Trustworthy-suspiciousTitForTat', [280, 280])\n",
      "('Patrick', [328, 258])              ('SPTFT-Trustworthy', [287, 267])\n",
      "('ResponsiveTFTMod', [192, 327])     ('TitForTwoTats-Flipper', [150, 400])\n",
      "('Patrick', [240, 325])              ('Cooperate-Flipper', [150, 400])\n",
      "('Patrick', [320, 200])              ('Smart-ResponsiveTFTMod', [264, 269])\n",
      "('Flipper', [205, 310])              ('Patrick-Trustworthy', [320, 200])\n",
      "('SPTFT', [310, 285])                ('Trustworthy-ResponsiveTFTMod', [192, 327])\n",
      "('SPTFT', [304, 294])                ('scummyPrisoner-Flipper', [205, 310])\n",
      "('AverageTitForTat', [302, 297])     ('Trustworthy-Flipper', [180, 330])\n",
      "('AverageTitForTat', [302, 297])     ('CheckAndChoose-Cooperate', [490, 15])\n",
      "('TFTMemory', [302, 297])            ('Smart-Cooperate', [494, 9])\n",
      "('TFTMemory', [302, 297])            ('dbTriCon-Flipper', [248, 253])\n",
      "('suspiciousTitForTat', [297, 302])  ('fifty_is_good-Cooperate', [498, 3])\n",
      "('suspiciousTitForTat', [297, 302])  ('dbTriCon-TFTMemory', [250, 250])\n",
      "('dbTriCon', [301, 296])             ('fifty_is_good-scummyPrisoner', [0, 500])\n",
      "('suspiciousTitForTat', [296, 301])  ('fifty_is_good-Defect', [0, 500])\n",
      "('Cooperate', [300, 300])            ('scummyPrisoner-Cooperate', [500, 0])\n",
      "('Cooperate', [300, 300])            ('TFTMemory-Flipper', [250, 250])\n",
      "('TitForTat', [300, 300])            ('TFTMemory-TitForTat', [250, 250])\n",
      "('TitForTat', [300, 300])            ('Cooperate-Defect', [0, 500])\n",
      "('Defect', [300, 50])                ('Flipper-TitForTat', [250, 250])\n",
      "('AverageTitForTat', [299, 54])      ('Flipper-suspiciousTitForTat', [250, 250])\n",
      "('TitForTat', [299, 299])            ('TitForTat-suspiciousTitForTat', [250, 250])\n",
      "('fifty_is_good', [298, 53])         ('fifty_is_good-ResponsiveTFTMod', [166, 331])\n",
      "('CheckAndChoose', [295, 290])       ('AverageTitForTat-fifty_is_good', [485, 10])\n",
      "('CheckAndChoose', [293, 68])        ('fifty_is_good-Smart', [17, 477])\n",
      "('suspiciousTitForTat', [288, 293])  ('SPTFT-Flipper', [244, 249])\n",
      "('TitForTat', [291, 291])            ('AverageTitForTat-CheckAndChoose', [471, 21])\n",
      "('SPTFT', [287, 267])                ('CheckAndChoose-Smart', [23, 468])\n",
      "('Trustworthy', [283, 278])          ('CheckAndChoose-fifty_is_good', [454, 29])\n",
      "('suspiciousTitForTat', [280, 280])  ('SPTFT-suspiciousTitForTat', [239, 244])\n",
      "('ResponsiveTFTMod', [264, 269])     ('Smart-Trustworthy', [336, 146])\n",
      "('ResponsiveTFTMod', [182, 267])     ('Patrick-Flipper', [260, 210])\n",
      "('Patrick', [195, 265])              ('SPTFT-TitForTat', [233, 233])\n",
      "('Patrick', [260, 210])              ('fifty_is_good-TFTMemory', [235, 230])\n",
      "('Flipper', [248, 253])              ('fifty_is_good-Patrick', [195, 265])\n",
      "('TFTMemory', [250, 250])            ('Flipper-ResponsiveTFTMod', [182, 267])\n",
      "('Flipper', [250, 250])              ('CheckAndChoose-Trustworthy', [411, 36])\n",
      "('TitForTat', [250, 250])            ('scummyPrisoner-Trustworthy', [406, 41])\n",
      "('TitForTat', [250, 250])            ('AverageTitForTat-Trustworthy', [413, 33])\n",
      "('suspiciousTitForTat', [250, 250])  ('fifty_is_good-Trustworthy', [418, 23])\n",
      "('suspiciousTitForTat', [250, 250])  ('Trustworthy-Defect', [20, 420])\n",
      "('Flipper', [244, 249])              ('dbTriCon-fifty_is_good', [222, 217])\n",
      "('suspiciousTitForTat', [239, 244])  ('fifty_is_good-suspiciousTitForTat', [217, 217])\n",
      "('fifty_is_good', [242, 172])        ('fifty_is_good-SPTFT', [219, 214])\n",
      "('fifty_is_good', [235, 230])        ('fifty_is_good-TitForTat', [218, 213])\n",
      "('TitForTat', [233, 233])            ('fifty_is_good-TitForTwoTats', [242, 172])\n",
      "('dbTriCon', [222, 217])             ('Smart-SPTFT', [206, 201])\n",
      "('fifty_is_good', [219, 214])        ('Smart-Flipper', [201, 201])\n",
      "('fifty_is_good', [218, 213])        ('Smart-TitForTat', [203, 198])\n",
      "('suspiciousTitForTat', [217, 217])  ('Smart-suspiciousTitForTat', [198, 203])\n",
      "('Smart', [206, 201])                ('CheckAndChoose-Flipper', [293, 68])\n",
      "('Smart', [203, 198])                ('AverageTitForTat-Flipper', [299, 54])\n",
      "('suspiciousTitForTat', [198, 203])  ('fifty_is_good-Flipper', [298, 53])\n",
      "('Flipper', [201, 201])              ('Defect-Flipper', [300, 50])\n",
      "('Patrick', [155, 135])              ('Patrick-SPTFT', [155, 135])\n",
      "('Patrick', [134, 154])              ('dbTriCon-Patrick', [134, 154])\n",
      "('dbTriCon', [141, 136])             ('dbTriCon-SPTFT', [141, 136])\n",
      "('scummyPrisoner', [128, 93])        ('CheckAndChoose-SPTFT', [125, 110])\n",
      "('CheckAndChoose', [127, 107])       ('CheckAndChoose-TFTMemory', [127, 107])\n",
      "('CheckAndChoose', [125, 110])       ('Smart-TitForTwoTats', [125, 105])\n",
      "('Smart', [125, 105])                ('dbTriCon-Smart', [106, 121])\n",
      "('AverageTitForTat', [124, 94])      ('CheckAndChoose-dbTriCon', [115, 110])\n",
      "('Smart', [106, 121])                ('CheckAndChoose-ResponsiveTFTMod', [108, 113])\n",
      "('Defect', [95, 120])                ('scummyPrisoner-SPTFT', [128, 93])\n",
      "('TFTMemory', [95, 120])             ('AverageTitForTat-SPTFT', [124, 94])\n",
      "('scummyPrisoner', [99, 119])        ('CheckAndChoose-scummyPrisoner', [99, 119])\n",
      "('CheckAndChoose', [115, 110])       ('CheckAndChoose-Defect', [95, 120])\n",
      "('ResponsiveTFTMod', [108, 113])     ('Patrick-Smart', [110, 105])\n",
      "('Defect', [97, 112])                ('SPTFT-TFTMemory', [95, 120])\n",
      "('Patrick', [110, 105])              ('AverageTitForTat-Patrick', [107, 102])\n",
      "('scummyPrisoner', [108, 98])        ('AverageTitForTat-TitForTat', [107, 102])\n",
      "('Defect', [98, 108])                ('Patrick-ResponsiveTFTMod', [102, 107])\n",
      "('Defect', [98, 108])                ('Smart-TFTMemory', [102, 107])\n",
      "('AverageTitForTat', [107, 102])     ('SPTFT-Defect', [97, 112])\n",
      "('AverageTitForTat', [107, 102])     ('SPTFT-ResponsiveTFTMod', [102, 107])\n",
      "('ResponsiveTFTMod', [102, 107])     ('TitForTat-ResponsiveTFTMod', [102, 107])\n",
      "('TFTMemory', [102, 107])            ('Patrick-TitForTat', [106, 101])\n",
      "('ResponsiveTFTMod', [102, 107])     ('AverageTitForTat-dbTriCon', [103, 103])\n",
      "('ResponsiveTFTMod', [102, 107])     ('AverageTitForTat-TFTMemory', [103, 103])\n",
      "('Patrick', [106, 101])              ('Patrick-suspiciousTitForTat', [103, 103])\n",
      "('AverageTitForTat', [104, 99])      ('scummyPrisoner-TitForTwoTats', [108, 98])\n",
      "('scummyPrisoner', [99, 104])        ('Smart-Defect', [98, 108])\n",
      "('Defect', [99, 104])                ('TitForTwoTats-Defect', [98, 108])\n",
      "('scummyPrisoner', [99, 104])        ('AverageTitForTat-Smart', [104, 99])\n",
      "('Defect', [99, 104])                ('dbTriCon-scummyPrisoner', [99, 104])\n",
      "('scummyPrisoner', [104, 99])        ('dbTriCon-Defect', [99, 104])\n",
      "('Defect', [104, 99])                ('Patrick-scummyPrisoner', [99, 104])\n",
      "('dbTriCon', [103, 103])             ('Patrick-Defect', [99, 104])\n",
      "('TFTMemory', [103, 103])            ('scummyPrisoner-TitForTat', [104, 99])\n",
      "('suspiciousTitForTat', [103, 103])  ('Defect-TitForTat', [104, 99])\n",
      "('scummyPrisoner', [100, 100])       ('AverageTitForTat-scummyPrisoner', [100, 100])\n",
      "('Defect', [100, 100])               ('AverageTitForTat-Defect', [100, 100])\n",
      "('ResponsiveTFTMod', [100, 100])     ('AverageTitForTat-ResponsiveTFTMod', [100, 100])\n",
      "('suspiciousTitForTat', [100, 100])  ('AverageTitForTat-suspiciousTitForTat', [100, 100])\n",
      "('ResponsiveTFTMod', [100, 100])     ('dbTriCon-ResponsiveTFTMod', [100, 100])\n",
      "('Smart', [100, 100])                ('scummyPrisoner-Smart', [100, 100])\n",
      "('TFTMemory', [100, 100])            ('scummyPrisoner-TFTMemory', [100, 100])\n",
      "('Defect', [100, 100])               ('scummyPrisoner-Defect', [100, 100])\n",
      "('ResponsiveTFTMod', [100, 100])     ('scummyPrisoner-ResponsiveTFTMod', [100, 100])\n",
      "('suspiciousTitForTat', [100, 100])  ('scummyPrisoner-suspiciousTitForTat', [100, 100])\n",
      "('Defect', [100, 100])               ('TFTMemory-Defect', [100, 100])\n",
      "('ResponsiveTFTMod', [100, 100])     ('TFTMemory-ResponsiveTFTMod', [100, 100])\n",
      "('suspiciousTitForTat', [100, 100])  ('TFTMemory-suspiciousTitForTat', [100, 100])\n",
      "('ResponsiveTFTMod', [100, 100])     ('Defect-ResponsiveTFTMod', [100, 100])\n",
      "('suspiciousTitForTat', [100, 100])  ('Defect-suspiciousTitForTat', [100, 100])\n",
      "('suspiciousTitForTat', [100, 100])  ('ResponsiveTFTMod-suspiciousTitForTat', [100, 100])\n"
     ]
    }
   ],
   "source": [
    "s_ind = sorted(t.match_scores, key=lambda x: max(x[1]), reverse=True)\n",
    "s_col = sorted(t.match_scores, key=lambda x: sum(x[1]), reverse=True)\n",
    "\n",
    "headers = ['Individual', 'Collective']\n",
    "\n",
    "tab_list = []\n",
    "for i in range(len(s_ind)):\n",
    "    split = s_ind[i][0].split('-')\n",
    "    if s_ind[i][1][0] > s_ind[i][1][1]:\n",
    "        rule = split[0]\n",
    "    else:\n",
    "        rule = split[1]\n",
    "    \n",
    "    indiv = (rule, s_ind[i][1])\n",
    "    tab_list.append([indiv, s_col[i]])\n",
    "    # tab_list.append([s_ind[i], s_col[i]])\n",
    "\n",
    "print(tabulate(tab_list, headers=headers))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Maximum Total Points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rule                   Total Points    Average Points\n",
      "-------------------  --------------  ----------------\n",
      "TitForTwoTats                  3659           228.688\n",
      "Smart                          3597           224.812\n",
      "Flipper                        3510           219.375\n",
      "CheckAndChoose                 3475           217.188\n",
      "TitForTat                      3365           210.312\n",
      "AverageTitForTat               3320           207.5\n",
      "suspiciousTitForTat            3245           202.812\n",
      "Patrick                        3206           200.375\n",
      "Trustworthy                    3055           190.938\n",
      "dbTriCon                       3050           190.625\n",
      "Defect                         2980           186.25\n",
      "fifty_is_good                  2979           186.188\n",
      "ResponsiveTFTMod               2964           185.25\n",
      "Cooperate                      2937           183.562\n",
      "TFTMemory                      2921           182.562\n",
      "SPTFT                          2894           180.875\n",
      "scummyPrisoner                 2878           179.875\n"
     ]
    }
   ],
   "source": [
    "max_list = []\n",
    "\n",
    "for rule, scores in t.rule_scores.items():\n",
    "    total = str(sum([score[0] for score in scores]))\n",
    "    avg = str(sum([score[0] for score in scores])/len(scores))\n",
    "    max_list.append([rule, total, avg])\n",
    "\n",
    "max_list.sort(key=lambda x: x[1], reverse=True)\n",
    "print(tabulate(max_list, headers=['Rule', 'Total Points', 'Average Points']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Point Differences\n",
    "Which rule won by the most points on average? \n",
    "Which lost by the least on average?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Won by Most                                    Lost by Least\n",
      "---------------------------------------------  ---------------------------------------\n",
      "['Defect', '101.47058823529412']               ['AverageTitForTat', '0.0']\n",
      "['Smart', '95.58823529411765']                 ['suspiciousTitForTat', '0.0']\n",
      "['AverageTitForTat', '94.41176470588235']      ['ResponsiveTFTMod', '0.0']\n",
      "['CheckAndChoose', '90.88235294117646']        ['Defect', '0.0']\n",
      "['scummyPrisoner', '85.0']                     ['Patrick', '1.1764705882352942']\n",
      "['fifty_is_good', '71.76470588235294']         ['Smart', '2.0588235294117645']\n",
      "['Flipper', '45.0']                            ['TitForTat', '2.3529411764705883']\n",
      "['ResponsiveTFTMod', '44.11764705882353']      ['scummyPrisoner', '6.176470588235294']\n",
      "['Patrick', '38.23529411764706']               ['dbTriCon', '9.117647058823529']\n",
      "['Trustworthy', '23.235294117647058']          ['SPTFT', '10.0']\n",
      "['SPTFT', '3.235294117647059']                 ['TFTMemory', '11.176470588235293']\n",
      "['TFTMemory', '2.3529411764705883']            ['TitForTwoTats', '45.88235294117647']\n",
      "['suspiciousTitForTat', '1.7647058823529411']  ['CheckAndChoose', '60.88235294117647']\n",
      "['dbTriCon', '0.8823529411764706']             ['Flipper', '64.70588235294117']\n",
      "['Cooperate', '0.0']                           ['Trustworthy', '148.8235294117647']\n",
      "['TitForTat', '0.0']                           ['fifty_is_good', '152.94117647058823']\n",
      "['TitForTwoTats', '0.0']                       ['Cooperate', '182.64705882352942']\n"
     ]
    }
   ],
   "source": [
    "t.rule_scores\n",
    "\n",
    "gr_diff_list = []\n",
    "le_diff_list = []\n",
    "l = len(t.rule_scores)\n",
    "for rule, scores in t.rule_scores.items():\n",
    "    grs = 0\n",
    "    les = 0\n",
    "    for score in scores:\n",
    "        if score[0] > score[1]:\n",
    "            grs += score[0] - score[1] \n",
    "        elif score[1] > score[0]:\n",
    "            les += score[1] - score[0]\n",
    "    gr_diff_list.append([rule, str(grs/l)])\n",
    "    le_diff_list.append([rule, str(les/l)])\n",
    "        \n",
    "gr_diff_list.sort(key=lambda x: float(x[1]), reverse=True)\n",
    "le_diff_list.sort(key=lambda x: float(x[1]))\n",
    "\n",
    "diff_list = []\n",
    "for i in range(len(gr_diff_list)):\n",
    "    diff_list.append([gr_diff_list[i], le_diff_list[i]])\n",
    "\n",
    "headers = ['Won by Most', 'Lost by Least']\n",
    "\n",
    "print(tabulate(diff_list, headers=headers))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
